package com.teamcenter.rac.massupdate.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import com.teamcenter.rac.massupdate.converters.OperationType2DisplayStringConverter;
import com.teamcenter.rac.massupdate.converters.Source2LabelConverter;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.model.UpdateResult;
import com.teamcenter.rac.viewer.listener.ColumnListener;
import com.teamcenter.rac.viewer.view.ViewerHelper;
@SuppressWarnings({"rawtypes"})
public class ResultsDialog extends AbstractMassUpdateDialog {
	protected Label m_changeObjectLabel;
	protected Text m_changeObjectText;
	protected Button m_manageMarkupButton;
	protected Text m_operationTypeText;
	protected Label m_replacementPartLabel;
	protected Text m_replacementPartText;
	private TableViewer m_tableViewer;
	protected Text m_targetPartText;
	protected UpdateJobModel m_updateJobModel;
	protected WritableList m_updateResults = new WritableList(new ArrayList(), UpdateResult.class);
	private Table m_updateResultsTable;

	public ResultsDialog(Shell paramShell, UpdateJobModel paramUpdateJobModel, List<UpdateResult> paramList) {
		super(paramShell);
		setShellStyle(34032);
		this.m_updateJobModel = paramUpdateJobModel;
		if (paramList != null) {
			Iterator localIterator = paramList.iterator();
			while (localIterator.hasNext()) {
				UpdateResult localUpdateResult = (UpdateResult) localIterator.next();
				this.m_updateResults.add(localUpdateResult);
			}
		}
	}

	protected void createButtonsForButtonBar(Composite paramComposite) {
		this.m_manageMarkupButton = createButton(paramComposite, 1060, Messages.MassUpdateDialog_Btn_ManageMarkup,
				false);
		this.m_manageMarkupButton.setToolTipText(Messages.ResultsDialog_manageMarkupButton_toolTipText);
		this.m_manageMarkupButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent) {
				ResultsDialog.this.setReturnCode(1060);
				ResultsDialog.this.close();
			}
		});
		this.m_manageMarkupButton.setVisible(isMarkupAttached());
		Button localButton = createButton(paramComposite, 12, IDialogConstants.CLOSE_LABEL, true);
		localButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent) {
				ResultsDialog.this.close();
			}
		});
		localButton.setFocus();
		this.m_bindingContext = initDataBindings();
		setupAggregateValidationStatus();
		initCommonDataBindings(this.m_bindingContext);
	}

	protected Control createContents(Composite paramComposite) {
		Control localControl = super.createContents(paramComposite);
		setTitle(Messages.ResultsDialog_Title);
		getShell().setText(Messages.ResultsDialog_Title);
		setMessage("");
		return localControl;
	}

	protected Control createDialogArea(Composite paramComposite) {
		Composite localComposite1 = (Composite) super.createDialogArea(paramComposite);
		localComposite1.setLayout(new GridLayout(1, false));
		Composite localComposite2 = new Composite(localComposite1, 0);
		localComposite2.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		localComposite2.setLayout(new GridLayout(1, false));
		contributeOperationalDataGroup(localComposite2);
		contributeResultsTableGroup(localComposite2);
		return localComposite1;
	}

	protected void initCommonDataBindings(DataBindingContext paramDataBindingContext) {
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext localDataBindingContext = new DataBindingContext();
		ISWTObservableValue localISWTObservableValue1 = SWTObservables.observeText(this.m_targetPartText, 24);
		IObservableValue localIObservableValue1 = BeansObservables.observeValue(this.m_updateJobModel, "targetPart");
		UpdateValueStrategy localUpdateValueStrategy1 = new UpdateValueStrategy();
		localUpdateValueStrategy1.setConverter(new Source2LabelConverter());
		localDataBindingContext.bindValue(localISWTObservableValue1, localIObservableValue1,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy1);
		ISWTObservableValue localISWTObservableValue2 = SWTObservables.observeText(this.m_operationTypeText, 24);
		IObservableValue localIObservableValue2 = BeansObservables.observeValue(this.m_updateJobModel, "operationType");
		UpdateValueStrategy localUpdateValueStrategy2 = new UpdateValueStrategy();
		localUpdateValueStrategy2.setConverter(new OperationType2DisplayStringConverter());
		localDataBindingContext.bindValue(localISWTObservableValue2, localIObservableValue2,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy2);
		ISWTObservableValue localISWTObservableValue3 = SWTObservables.observeText(this.m_replacementPartText, 24);
		IObservableValue localIObservableValue3 = BeansObservables.observeValue(this.m_updateJobModel,
				"replacementPart");
		UpdateValueStrategy localUpdateValueStrategy3 = new UpdateValueStrategy();
		localUpdateValueStrategy3.setConverter(new Source2LabelConverter());
		localDataBindingContext.bindValue(localISWTObservableValue3, localIObservableValue3,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy3);
		ISWTObservableValue localISWTObservableValue4 = SWTObservables.observeText(this.m_changeObjectText, 24);
		IObservableValue localIObservableValue4 = BeansObservables.observeValue(this.m_updateJobModel, "changeObject");
		UpdateValueStrategy localUpdateValueStrategy4 = new UpdateValueStrategy();
		localUpdateValueStrategy4.setConverter(new Source2LabelConverter());
		localDataBindingContext.bindValue(localISWTObservableValue4, localIObservableValue4,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy4);
		ISWTObservableValue localISWTObservableValue5 = SWTObservables.observeText(this.m_replacementPartLabel);
		IObservableValue localIObservableValue5 = BeansObservables.observeValue(this.m_updateJobModel,
				"inputLabelTextWithColon");
		localDataBindingContext.bindValue(localISWTObservableValue5, localIObservableValue5,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		ISWTObservableValue localISWTObservableValue6 = SWTObservables.observeVisible(this.m_changeObjectLabel);
		IObservableValue localIObservableValue6 = BeansObservables.observeValue(this.m_updateJobModel,
				"changeSupported");
		localDataBindingContext.bindValue(localISWTObservableValue6, localIObservableValue6,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		ISWTObservableValue localISWTObservableValue7 = SWTObservables.observeVisible(this.m_changeObjectText);
		localDataBindingContext.bindValue(localISWTObservableValue7, localIObservableValue6,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		return localDataBindingContext;
	}

	private void contributeActualTableColumns(TableViewer paramTableViewer) {
		String[] arrayOfString1 = { "object_string", "resultStatus", "resultComment" };
		int[] arrayOfInt = { 160, 120, 300 };
		String[] arrayOfString2 = { Messages.ResultsDialog_Col_ImpactedPart, Messages.ResultsDialog_Col_Status,
				Messages.ResultsDialog_Col_Comments };
		ViewerHelper.addColumns(paramTableViewer, arrayOfString1, arrayOfString2);
		int i = this.m_updateResultsTable.getColumnCount();
		for (int j = 0; j < i; j++) {
			TableColumn localTableColumn = this.m_updateResultsTable.getColumn(j);
			localTableColumn.setMoveable(true);
			localTableColumn.setResizable(true);
			localTableColumn.setWidth(arrayOfInt[j]);
			ColumnListener localColumnListener = new ColumnListener(paramTableViewer);
			localTableColumn.addSelectionListener(localColumnListener);
			localTableColumn.addControlListener(localColumnListener);
		}
	}

	protected void contributeOperationalDataGroup(Composite paramComposite) {
		GridLayout localGridLayout1 = new GridLayout(1, false);
		localGridLayout1.verticalSpacing = 0;
		localGridLayout1.marginWidth = 0;
		localGridLayout1.marginHeight = 0;
		localGridLayout1.horizontalSpacing = 0;
		Group localGroup = new Group(paramComposite, 0);
		localGroup.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		localGroup.setText(Messages.ResultsDialog_Grp_OperationalData);
		localGroup.setLayout(localGridLayout1);
		GridLayout localGridLayout2 = new GridLayout(2, false);
		localGridLayout2.marginLeft = 10;
		Composite localComposite = new Composite(localGroup, 0);
		localComposite.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		localComposite.setLayout(localGridLayout2);
		Label localLabel1 = new Label(localComposite, 0);
		FontData localFontData1 = localLabel1.getFont().getFontData()[0];
		localLabel1.setFont(JFaceResources.getFontRegistry().getBold(localFontData1.getName()));
		localLabel1.setText(Messages.ResultsDialog_TargetPart);
		this.m_targetPartText = new Text(localComposite, 8);
		this.m_targetPartText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		Label localLabel2 = new Label(localComposite, 0);
		FontData localFontData2 = localLabel2.getFont().getFontData()[0];
		localLabel2.setFont(JFaceResources.getFontRegistry().getBold(localFontData2.getName()));
		localLabel2.setText(Messages.ResultsDialog_OperationType);
		this.m_operationTypeText = new Text(localComposite, 8);
		this.m_operationTypeText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		this.m_replacementPartLabel = new Label(localComposite, 0);
		GridData localGridData = new GridData(16384, 16777216, false, false, 1, 1);
		localGridData.widthHint = 90;
		this.m_replacementPartLabel.setLayoutData(localGridData);
		FontData localFontData3 = this.m_replacementPartLabel.getFont().getFontData()[0];
		this.m_replacementPartLabel.setFont(JFaceResources.getFontRegistry().getBold(localFontData3.getName()));
		this.m_replacementPartLabel.setText(Messages.ResultsDialog_ReplacementPart);
		this.m_replacementPartText = new Text(localComposite, 8);
		this.m_replacementPartText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		this.m_changeObjectLabel = new Label(localComposite, 0);
		FontData localFontData4 = this.m_changeObjectLabel.getFont().getFontData()[0];
		this.m_changeObjectLabel.setFont(JFaceResources.getFontRegistry().getBold(localFontData4.getName()));
		this.m_changeObjectLabel.setText(Messages.ResultsDialog_ChangeObject);
		this.m_changeObjectText = new Text(localComposite, 8);
		this.m_changeObjectText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
	}

	protected void contributeResultsTableGroup(Composite paramComposite) {
		GridLayout localGridLayout1 = new GridLayout(1, false);
		localGridLayout1.verticalSpacing = 0;
		localGridLayout1.marginWidth = 0;
		localGridLayout1.marginHeight = 0;
		localGridLayout1.horizontalSpacing = 0;
		Group localGroup = new Group(paramComposite, 0);
		localGroup.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		localGroup.setText(Messages.ResultsDialog_Grp_SelectedImpactedParts);
		localGroup.setLayout(localGridLayout1);
		GridLayout localGridLayout2 = new GridLayout(1, false);
		localGridLayout2.verticalSpacing = 0;
		localGridLayout2.marginWidth = 0;
		localGridLayout2.marginHeight = 0;
		localGridLayout2.horizontalSpacing = 0;
		Composite localComposite = new Composite(localGroup, 0);
		localComposite.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		localComposite.setLayout(localGridLayout2);
		this.m_tableViewer = new TableViewer(localComposite, 2820);
		this.m_tableViewer.setComparator(new AbstractMassUpdateDialog.IC_PropertyColumnViewerSorter());
		this.m_updateResultsTable = this.m_tableViewer.getTable();
		this.m_updateResultsTable.setLinesVisible(true);
		this.m_updateResultsTable.setHeaderVisible(true);
		this.m_updateResultsTable.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		contributeActualTableColumns(this.m_tableViewer);
		this.m_tableViewer.setContentProvider(new ObservableListContentProvider());
		this.m_tableViewer.setInput(this.m_updateResults);
	}

	protected boolean isMarkupAttached() {
		return (this.m_updateJobModel != null) && (this.m_updateJobModel.getMarkupObject() != null);
	}
}
